package org.water.garden.msn.service;

import java.util.HashMap;
import java.util.Map;

import net.sf.jml.Email;
import net.sf.jml.MsnMessenger;
import net.sf.jml.MsnProtocol;
import net.sf.jml.MsnUserStatus;
import net.sf.jml.impl.MsnMessengerFactory;

import org.water.garden.R;
import org.water.garden.activity.account.AccountList;
import org.water.garden.msn.MsnConstant;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.util.Log;

public class MsnService extends Service {

	private final static String LOG_TAG = "MsnService";
	private Map<Integer, MsnMessenger> messengers = new HashMap<Integer, MsnMessenger>();
    private NotificationManager notificationManager;

	public NotificationManager getNotificationManager() {
		return notificationManager;
	}
	
	public void notifyInfo(Notification notification){
		notificationManager.notify(MsnConstant.NOTFI_CODE_STATUS, notification);
	}

	public void cancelNotify() {
		notificationManager.cancel(MsnConstant.NOTFI_CODE_STATUS);
	}

	@Override
	protected void onStart(int startId, Bundle bundle) {
		super.onStart(startId, bundle);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		for (MsnMessenger msn : messengers.values()) {
			msn.logout();
		}
		cancelNotify();
	}

	@Override
	protected void onCreate() {
		super.onCreate();
		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}

	private final IMsnService.Stub binder = new IMsnService.Stub() {

		public int login(int accountId, int providerId, String accountName, String password) throws DeadObjectException {
			if(messengers.containsKey(accountId)) return 1;
			try {
				MsnMessenger messenger = MsnMessengerFactory.createMsnMessenger(accountName, password);
				messenger.setSupportedProtocol(new MsnProtocol[] { MsnProtocol.MSNP12 });
				messenger.getOwner().setInitStatus(MsnUserStatus.ONLINE);
				//messenger.setLogIncoming(true);
				//messenger.setLogOutgoing(true);
				
				messenger.addListener(new MsnActionAdapter(MsnService.this, messenger, accountId, providerId));
				
				notifyInfo(new Notification(
		        		   MsnService.this,
	                       R.drawable.msn_login, 
	                       getText(R.string.msn_account_logining),
	                       System.currentTimeMillis(), 
	                       getText(R.string.msn_account_logining),
	                       getText(R.string.msn_account_logining),
	                       new Intent(MsnService.this, AccountList.class), 
	                       R.drawable.msn_login,
	                       getText(R.string.msn_account_logining),
	                       new Intent(MsnService.this, AccountList.class)));
		        
				messenger.login();
				
				Log.i(LOG_TAG, "Login for account : " + accountId);
				messengers.put(accountId, messenger);
				return 1;
			} catch (Exception e) {
				Log.e(getClass().getName(), "MSN ERROR", e);
			}
			return 0;
		}

		public int logout(int accountId) throws DeadObjectException {
			if(messengers.containsKey(accountId)){
				
				Log.i(LOG_TAG, "Logout for account : " + accountId);
				messengers.get(accountId).logout();
				messengers.remove(accountId);
			}
			return 0;
		}

		public int sendText(int accountId, String contact, String content)
				throws DeadObjectException {
			if(messengers.containsKey(accountId)){
				messengers.get(accountId).sendText(Email.parseStr(contact), content);				
			}
			return 0;
		}

	};
}
